<?php
namespace app\common;
/**
 * @Author: K.EL
 * @Date:   2020-07-16 22:08:50
 * @Last Modified by:   K.EL
 * @Last Modified time: 2020-07-22 17:38:36
 */
class Util{
	public static $instance;
	public static function init(){
		if (!isset(self::$instance)) {//判断是否以前创建了当前类的实例
            self::$instance = new static;//如果没有创建，实例化当前类，这里实现类只实例化一次
        }
        return self::$instance;//返回类的实例
	}
    //生成唯一的UUID
	function UUID($prefix=''){
		$uuid = '';
        if (function_exists('uuid_create') === true){
            $uuid = uuid_create(1);
        }else{
            $data = openssl_random_pseudo_bytes(16);
            $data[6] = chr(ord($data[6]) & 0x0f | 0x40); 
            $data[8] = chr(ord($data[8]) & 0x3f | 0x80); 
            $uuid =  vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
        }
        return $prefix.$uuid;
	}

    //只获取parent_id=0的菜单
    function getTopMenu($menu=[]){
        $topMenu = [];
        $moreMenu = [];
        foreach($menu as $v){
            if($v['parent_id']==0 && $v['is_show']==1){
                if(count($topMenu)>=9){
                    $moreMenu[] = $v;
                }else{
                    $topMenu[] = $v; 
                }
            }
        }
        return ['topMenu'=>$topMenu,'moreMenu'=>$moreMenu];
    }

    //获取菜单树结构
    function getMenuTree($data, $field='parent_id', $id = 0, $lev = 0, $is_show=0,$key_to_id=1, $ln='child',$show_ln=0){
        $arr = [];
        if(!$data){
            return false;
        }
        foreach($data as $k=>$v){
            if(!$is_show){
                if($v['is_show']!=1){
                    continue;
                }
            }
            if($v[$field] == $id){
                $v['lev'] = $lev;
                $v[$ln] = self::getMenuTree($data, $field, $v['id'], $lev+1,$is_show,$key_to_id,$ln);
                if(!$v[$ln] && !$show_ln){
                    unset($v[$ln]);
                }else{
                    $v[$ln.'_count'] = count($v[$ln]);
                }
                if($key_to_id){
                    $arr[$v['id']] = $v;
                }else{
                    $arr[] = $v;
                }
            }
        }
        return $arr;
    }

    function getChu($len = 6, $all = 'all', $prefix=''){
        if($all == 'all'){
            $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
            //asdfad
        }

        if($all == 'number'){
            $str = '012345678901234567890123456789012345678901234567890123456789';
        }
        
        if($all == 'char'){
            $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
        }

        if($all == 'low'){
            $str = "23456789abcdefghjkminpqrstuvwxyz";
        }

        if($all == 'cas'){
            $str = "ABCDEFGHJKLMNPQRSTUVWXYZ123456789";
        }
        
        $rts = '';
        for($i=0;$i<$len;$i++){
            $shuffled = str_shuffle($str);
            $rts .= substr($shuffled, 0, 1);
        }
        return $prefix.$rts;
    }
}
